Create 373. Find K Pairs with Smallest Sums.md#10
Open
hiroki-horiguchi-dev wants to merge 2 commits intomainfrom
Open
Create 373. Find K Pairs with Smallest Sums.md#10hiroki-horiguchi-dev wants to merge 2 commits intomainfrom
hiroki-horiguchi-dev wants to merge 2 commits intomainfrom
Conversation
Shunii85
reviewed
Mar 24, 2026
| ## 1st | ||
| - 30分 | ||
| - nums1, nums2 の配列サイズ以上の k が与えられた時を0から考えつくのは初見は難しい気がする、これは答えみないとわからんよ。。 | ||
| - 勘違いして解いてしまったけど備忘録として残しておく |
There was a problem hiding this comment.
これじゃ解けないのだろうか?と疑問に思ったのですが、smallestにすべて追加し終わっても取り出しが間に合っていないのだと思います。
つまり、最大k = nums1の長さの組み合わせまでしか出力できないのだと思います。LeetCodeで確認したところ、そうなっていました。
|
|
||
| // ここが肝. i = 0, j = 0 が1周目のループで確定していて、i = 1 ~ i.length, j = 0 しか smallestQueue には入っていない | ||
| // 次の最小値のペアとして考えられるのは i = 0, j = 1 なので、これを smallestQueue 保存する | ||
| // 2周目以降がちょっと追いづらくて、poll() された要素の i, j + 1 を次の候補とするので、書き出したケースによっては意味不明な当たり方をしているように見えてしまい、最初の理解でつまづきがち |
There was a problem hiding this comment.
自分も理解があやふやな気がしたので、スライドを作ってみました。もしよければ見てみてください。
一行(もしくは一列)先に追加してしまう解き方のほうが理解しやすいですね。
| throw new IllegalArgumentException("サイズ1以上の配列を渡してください"); | ||
| } | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| PriorityQueue<int[]> indexPairQueue = new PriorityQueue<>((a, b) -> a[0] - b[0]); |
nodchip
reviewed
Mar 25, 2026
| smallest.add(new int[]{num1 + num2, num1, num2}); | ||
| } | ||
| int[] temp = smallest.poll(); | ||
| List<Integer> pair = new ArrayList<>(); |
There was a problem hiding this comment.
List.of() を使うとシンプルに書けると思いました。
https://docs.oracle.com/javase/jp/9/docs/api/java/util/List.html#of-E-E-
result.add(List.of(temp[1], temp[2]));
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
373. Find K Pairs with Smallest Sumsを解きました。
レビューお願いします。